//
//  PopoverPresentationManager.swift
//  WBSwift
//
//  Created by 贺瑞 on 2017/8/3.
//  Copyright © 2017年 herui. All rights reserved.
//

import UIKit
import SnapKit

class PopoverPresentationManager: UIPresentationController {
    var forPresent  = false
    lazy var cover = UIView()

    var block : ((_ forPresent : Bool)->())?
    
}


// MARK: - 显示大小+背景蒙层
extension PopoverPresentationManager {
    
    override func containerViewWillLayoutSubviews() {
        setupCover()
        
        presentedView?.frame = CGRect(x: 0, y: 0, width: 200, height: 300)
        
        let centerX = (containerView?.bounds.size.width ?? 0) * 0.5
        let centerY = CGFloat(50)
        presentedView?.center = CGPoint(x: centerX, y: centerY)
    }
    
    func setupCover() {
        
        containerView?.insertSubview(cover, at: 0)
        cover.snp.makeConstraints { (make : ConstraintMaker) in
            make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0))
        }
        
        cover.addGestureRecognizer(UITapGestureRecognizer(target: self, action:#selector(tapCover)))
        cover.backgroundColor = UIColor(white: 1, alpha: 0.6)
    }
    
    func tapCover() {
        presentedViewController.dismiss(animated: true, completion: nil)
    }
}


// MARK: - 显示+隐藏动画
extension PopoverPresentationManager : UIViewControllerAnimatedTransitioning {
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.3
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        if(forPresent){
            presentAnimateTransition(using: transitionContext)
        }else{
            dismissAnimateTransition(using: transitionContext)
        }
    }
    
    func presentAnimateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let toVC = transitionContext.viewController(forKey:.to)
        if let toVC = toVC {
            
            toVC.view.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
            toVC.view.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            transitionContext.containerView.addSubview(toVC.view)
            transitionContext.containerView.alpha = 0
            
            UIView.animate(withDuration: 0.3, animations: {
                toVC.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
                transitionContext.containerView.alpha = 1
            }, completion: { (completion) in
                transitionContext.completeTransition(true)
            })
        }
    }

    func dismissAnimateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let fromVC = transitionContext.viewController(forKey:.from)
        if let fromVC = fromVC {
            
            UIView.animate(withDuration: 0.3, animations: {
                fromVC.view.bounds = CGRect(x: 0, y: 0, width: 200, height: 0)
                transitionContext.containerView.alpha = 0
            }, completion: { (completion) in
                transitionContext.completeTransition(true)
            })
        }
    }
    
}

// MARK: - 代理方法
extension PopoverPresentationManager : UIViewControllerTransitioningDelegate{
    
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return self
    }
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        forPresent = true
        if let block = block {
           block(forPresent)
        }
        return self
    }
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        forPresent = false
        if let block = block {
            block(forPresent)
        }
        return self
    }
}
